package com.kcs.fxmargin.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.PopupCustomerActionForm;
import com.kcs.fxmargin.bo.CustomerInformationBO;
import com.kcs.fxmargin.model.MCustInfo;
import com.kcs.fxmargin.service.CustInfoService;

public class PopupCustomerAction extends Action {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	CustInfoService service = new CustInfoService();

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		try{

			log.debug("PopupCustomerAction : Start");
			
			PopupCustomerActionForm formBean = (PopupCustomerActionForm) form;
			
			if("init".equals(formBean.getpAction())) {
				formBean.resetExpect();
				formBean.setCustomerInformationBOList(new ArrayList<CustomerInformationBO>());
			} else if("search".equals(formBean.getpAction())) search(formBean, request);
			
			log.debug("PopupCustomerAction : End");	
		}catch(Exception e){
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}
		
		
		return mapping.findForward("popupCustomer");
	}
	
	public void search(PopupCustomerActionForm formBean, HttpServletRequest request) throws Exception {
		log.debug("PopupCustomerAction : search : Start");
		
		MCustInfo custInfo = new MCustInfo();
		custInfo.setCifNo(formBean.getpCifNo());
		custInfo.setCustName(formBean.getpCustomerName());
		
		formBean.setCustomerInformationBOList(service.searchPopupCustomer(custInfo));
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("PopupCustomerAction : search : End");
	}

}
